import Image
from numpy import array
from kmeans import *


def listOfPixels(im):
    # transforma os dados de uma imagem em uma lista de arrays (do numpy)
    return map(array,map(list,list(im.getdata())))


def histogram(codebook,pixelist):
    # codebook and pixelist are list of array (from numpy) of integer
    k = len(codebook)
    if(k<2):
        return
    cb_index=range(k) #lista com o indice dos elementos do codebook
    hist=[0.0]*k # lista inicial zerada com os valores do histograma
    
    for pixel in pixelist:
        dist_lst = [dist_l2(pixel,codeword) for codeword in codebook]
        dist_index = zip(dist_lst,cb_index)
        selected = min(dist_index)
        ind = selected[1]
        hist[ind]=hist[ind]+1
    return hist
    
    
